import React, { Component } from 'react'
import { withRouter } from 'react-router-dom'

class Header extends Component {

  back = () => {
    this.props.history.goBack()
  }

  forward = () => {
    this.props.history.goForward()
  }

  go = (num) => {
    return () => {
      console.log(num)
      this.props.history.go(num);
    }
  }

  render() {
    console.log('这是Header组件接收到的props', this.props);
    return (
      <div>
        <h2>This is React-Router-Dom页面</h2>
        <button onClick={this.back}>回退</button>&nbsp;
        <button onClick={this.forward}>前进</button>&nbsp;
        <button onClick={this.go(-1)}>Go 倒退1步 -1</button>&nbsp;
        <button onClick={this.go(1)}>Go 前进1步 1</button>&nbsp;
      </div>
    )
  }
}

export default  withRouter(Header);

// withRouter可以加工一般组件，让一般组件具备路由组件所特有的API
// withRouter的返回值是一个新组件，这个新组件会向一般组件传递history、location、match三个属性